﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common;
using Dln.Common.Helper;
using ServiceStack;
using ServiceStack.Logging;
using DynamicJson = Dln.Common.Helper.DynamicJson;
using Dln.Handler;

public partial class GetOpenid : BasePage
{
    protected string AppId = CommonHelper.GetAppSettings("AppID");
    protected string AppSecret = CommonHelper.GetAppSettings("AppSecret");
    protected static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    protected string openid = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        openid = GetAuthOpenId();
        //openid = "oF6dUwxAa6BtWPqaRnE_lz7lDBgQ";
        //var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid);
        //if (uptModel == null && uptModel.Count() <= 0)
        //{
        //    ResponseToPage("请关注德朗诺公众号！");
        //}
        //else
        //{
        //    WeChatUsers model = uptModel.ToFirstDefault();

        //    if (model.RoleType == 0 && !string.IsNullOrEmpty(model.CardID))
        //    {
        //        this.Response.Redirect("/page/Community/DoctorIndex.aspx");
        //    }
        //    if (model.RoleType == 1 && !string.IsNullOrEmpty(model.CardID) && !string.IsNullOrEmpty(model.PatientID))
        //    {
        //        this.Response.Redirect("/page/Community/PatientIndex.aspx");
        //    }
        //}

        //var returnUrl = DNTRequest.GetString("return_url");

        //var code = DNTRequest.GetString("code");
        //if (!string.IsNullOrWhiteSpace(code))
        //{
        //    Log.WarnFormat("Line28,code={0}", code);
        //    var customerOpenId = GetOpenId(code);

        //    Response.Cookies.Add(new HttpCookie(CommonHelper.GetAppSettings("CustomerOpenId"))
        //    {
        //        Value = customerOpenId,
        //        Expires = DateTime.Now.AddDays(1)
        //    });
        //}
        //else
        //{
        //    var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2FGetOpenid.aspx&response_type=code&scope=snsapi_base&state=test#wechat_redirect", AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
        //    Log.WarnFormat("Line43,url={0}", url);
        //    Response.Redirect(url, true);
        //}
    }

    //protected string GetOpenId(string code)
    //{
    //    string tokenurl = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code);
    //    string json = RequestHelper.SendGet(tokenurl);
    //    if (!String.IsNullOrEmpty(json))
    //    {
    //        openid = json;
    //        ClientScript.RegisterStartupScript(this.GetType(), " ", "alert('" + json + "');", true);
    //        Log.WarnFormat("Line54,json={0}", json);

    //        try
    //        {
    //            var token = DynamicJson.Parse(json);
    //            Session["openid"] = token.openid;

    //            return token.openid;
    //        }
    //        catch (Exception ex)
    //        {
    //            Log.WarnFormat(ex.ToString());

    //            return null;
    //        }
    //    }
    //    return null;
    //}
}